package com.ptmall.app.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.bean.WalletRechargeBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.WalletListAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WalletListActivity extends BaseMvpActivity{

    PullableListView mListview;
    PullToRefreshLayout mRefreshView;

    TitleBarView titlebar;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    int page = 1;

    List<WalletRechargeBean> mDataLists = new ArrayList<>();

    WalletListAdapter mWalletListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_walletlist);

    }

    @Override
    public void initViews() {
        titlebar= (TitleBarView) findViewById(R.id.titlebar);
        mRefreshView = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        mListview = (PullableListView) findViewById(R.id.listview);

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page ++;
                getData();
            }
        });

        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {
        mWalletListAdapter = new WalletListAdapter(getContext(),null);
        mListview.setAdapter(mWalletListAdapter);
    }

    private void getData(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        apiDataRepository.getWalletList(map, new ApiNetResponse<PageData<WalletRechargeBean>>() {
            @Override
            public void onSuccess(PageData<WalletRechargeBean> datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas && datas.getData().size() > 0) {
                        mDataLists.addAll(datas.getData());
                    } else {
                    }
                } else {
                    if (null != datas && datas.getData().size() > 0) {
                        mDataLists.addAll(datas.getData());
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mWalletListAdapter.pushData(mDataLists);
            }
        });
    }
}
